﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.ZP;
using Rison.BusinessFacade.Facade.ZP;
using Rison.DataAccess.Common;

namespace Rison.Web.ZP
{
    public partial class SelfExaminationDetail : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!string.IsNullOrEmpty(departid))
                {
                    LoadInfo();
                    BindData();
                }                               
            }

            
         }

        private void LoadInfo()
        {
            //string departid = departid;
            HiddentYear.Value = year;

            SelfExaminationFacade facade = new SelfExaminationFacade();
            DataRow dr = facade.GetInformation(departid,Convert.ToInt32(year));
            if (dr != null)
            {
                HiddenID.Value = dr["ID"].ToString();

                this.txtCompanyName.Text =dr["CompanyName"].ToString() ;
                this.txtZGKS.Text = dr["ZGKS"].ToString();
                this.txtFGLD.Text = dr["FGLD"].ToString();
                this.txtFGLDTelephone.Text = dr["FGLDTelephone"].ToString();
                this.txtFGLDMobile.Text = dr["FGLDMobile"].ToString();
                this.txtFGLDEmail.Text = dr["FGLDEmail"].ToString();
                this.txtWZFZR.Text = dr["WZFZR"].ToString();
                this.txtWZFZRTelephone.Text = dr["WZFZRTelephone"].ToString();
                this.txtWZFZRMobile.Text = dr["WZFZRMobile"].ToString();
                this.txtWZFZREmail.Text = dr["WZFZREmail"].ToString();
                this.txtAQLLR.Text = dr["AQLLR"].ToString();
                this.txtAQLLRTelephone.Text = dr["AQLLRTelephone"].ToString();
                this.txtAQLLRMobile.Text = dr["AQLLRMobile"].ToString();
                this.txtAQLLREmail.Text = dr["AQLLREmail"].ToString();
            }
        }

        private string departid
        {
            get { return string.IsNullOrEmpty(Request.QueryString["DepartID"]) ? "4f525500-fd00-428a-b625-bc0ce15b5cca" : Request.QueryString["DepartID"]; }
        }

        private string year
        {
            get { return string.IsNullOrEmpty(Request.QueryString["year"]) ? System.DateTime.Today.Year.ToString() : Request.QueryString["DepartID"]; }
        }

        protected override string key
        {
            get
            {
                if (HiddenID.Value != "")
                {
                    return HiddenID.Value;
                }
                return "";
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SelfExaminationFacade facade = new SelfExaminationFacade();

            //List<SqlField> indexKeyFields = new List<SqlField>();
            //indexKeyFields.Add(new SqlField("ID", txtUserId.Text.Trim()));

            
            SelfExamination exam = null;
            if (HiddenID.Value != "")
                exam = facade.Retrieve("id",key) as SelfExamination;
            else
                exam = new SelfExamination();

            if (HiddenID.Value != "")
                exam.ID = HiddenID.Value;
            else
                exam.ID =System.Guid.NewGuid().ToString();

            if (HiddentYear.Value!="")
                exam.Year =Convert.ToInt32(HiddentYear.Value);
            else
                exam.Year=System.DateTime.Today.Year;


            exam.DepartID = departid;
            exam.FieldTotal = 62;
            exam.FieldNum =0;
            exam.CompanyName=this.txtCompanyName.Text;
            exam.ZGKS=this.txtZGKS.Text;
            exam.FGLD=this.txtFGLD.Text;
            exam.FGLDTelephone=this.txtFGLDTelephone.Text;
            exam.FGLDMobile=this.txtFGLDMobile.Text;
            exam.FGLDEmail=this.txtFGLDEmail.Text;
            exam.WZFZR=this.txtWZFZR.Text;
            exam.WZFZRTelephone=this.txtWZFZRTelephone.Text;
            exam.WZFZRMobile=this.txtWZFZRMobile.Text;
            exam.WZFZREmail=this.txtWZFZREmail.Text;
            exam.AQLLR=this.txtAQLLR.Text;
            exam.AQLLRTelephone=this.txtAQLLRTelephone.Text;
            exam.AQLLRMobile=this.txtAQLLRMobile.Text;
            exam.AQLLREmail = this.txtAQLLREmail.Text;
            
            long returnValue = 0;
            if (HiddenID.Value!="")
            {
                returnValue = facade.Update(exam);
            }
            else
                returnValue = facade.Save(exam);

            if (returnValue > 0)
            {
                Response.Write("<script>alert('保存完成！')</script>");
            }
            else
                Response.Write("<script>alert('保存报错！')</script>");

        }


        #region "create/delete/update child websit"      
  
        protected void fwWebsite_ModeChanging(object sender, FormViewModeEventArgs e)
        {
            //判断模式 
            if (e.NewMode == FormViewMode.Insert)
            {
                fwWebsite.ChangeMode(FormViewMode.Insert);
            }
            else if (e.NewMode == FormViewMode.Edit)
            {
                fwWebsite.ChangeMode(FormViewMode.Edit);
            }
            else if (e.CancelingEdit)
            {
                fwWebsite.ChangeMode(FormViewMode.ReadOnly);
            }
            BindData();
        }

        protected void fwWebsite_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            TextBox TextBox1 = (TextBox)this.fwWebsite.FindControl("TextBox10");
            TextBox TextBox2 = (TextBox)this.fwWebsite.FindControl("TextBox11");

            DataTable temp = dtWebsite;
            DataRow dr = temp.NewRow();
            dr["website"] = TextBox1.Text;
            dr["domainname"] = TextBox2.Text;           
            temp.Rows.Add(dr);

            dtWebsite = temp;//覆盖数据池

            fwWebsite.ChangeMode(FormViewMode.Edit);
            BindData();
        }

        protected void fwWebsite_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {

        }

        private void BindData()
        {
            //DataTable dt = new DataTable();
            fwWebsite.DataSource = dtWebsite;
            fwWebsite.DataBind();
        }

        private DataTable dtWebsite
        {
            get 
            {
                DataTable dt = new DataTable("website");
                if (ViewState["website"] != null)
                {
                    dt = (DataTable)ViewState["website"];
                }
                else
                {                 
                    dt.Columns.Add("website");
                    dt.Columns.Add("domainname");
                    DataRow dr = dt.NewRow();
                    dr["website"] = 1;
                    dr["domainname"] = 1;
                    dt.Rows.Add(dr);
                    dr = dt.NewRow();
                    dr["website"] = 2;
                    dr["domainname"] = 2;
                    dt.Rows.Add(dr);


                    ViewState["tmp"] = dt; //创建数据池
                }
                return dt;
            }
            set 
            {
                ViewState["website"] = dtWebsite;
            }
        }
      

        #endregion

        

    }
}
